﻿ using JHWpfToolkit;
using MS.Common;
using MS.Common.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Reflection;
using Prism.Regions;
using Wpf.Models;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using JHECIS.NursingRecord.DataModels;
using JHECIS.NursingRecord.Common;
using Newtonsoft.Json;
using System.Windows.Controls;

namespace wpf.Nursing.ViewModels
{

    /// <summary>
    /// 护理记录批量录入ViewModel
    /// </summary>
    public class BatchVitalSignViewModel : VmBase, INavigationAware
    {
        #region 业务操作
       
        readonly Notifiaction notifiaction = Notifiaction.Instance;
        #endregion

        #region 属性

        /// <summary>
        ///  当前编辑的单元格项目编码  2023-05-24
        /// </summary>
        private VitalSignRecord EditObj { get; set; }

        private bool _CanEditData = true;
        /// <summary>
        /// 是否可修改搜索条件  2023-05-24
        /// </summary>
        public bool CanEditData
        {
            get
            {
                return _CanEditData;
            }
            set
            {
                _CanEditData = value;
                RaisePropertyChanged("CanEditData");
            }
        }

        private DateTime _ChooseDATE;
        /// <summary>
        /// 选择的录入时间  2023-05-24
        /// </summary>
        public DateTime ChooseDATE
        {
            get
            {
                return _ChooseDATE;
            }
            set
            {
                _ChooseDATE = value;
                RaisePropertyChanged("ChooseDATE");
            }
        }

        private List<VitalSignRecord> _AllVitalSignRecord;
        /// <summary>
        /// DataGrid绑定列表数据  2023-05-24
        /// </summary>
        public List<VitalSignRecord> AllVitalSignRecord
        {
            get
            {
                return _AllVitalSignRecord;
            }
            set
            {
                _AllVitalSignRecord = value;
                RaisePropertyChanged("AllVitalSignRecord");
            }
        }
        private VitalSignRecord _CurrVitSign;
        /// <summary>
        /// 当前选中列表数据  2023-05-24
        /// </summary>
        public VitalSignRecord CurrVitSign
        {
            get
            {
                return _CurrVitSign;
            }
            set
            {
                _CurrVitSign = value;
                CurrNursingVal = CurrVitSign?.NURSING;
                RaisePropertyChanged("CurrVitSign");
            }
        }
        private string _CurrNursingVal;
        /// <summary>
        /// 当前修改后的护理内容  2023-05-24
        /// </summary>
        public string CurrNursingVal
        {
            get
            {
                return _CurrNursingVal;
            }
            set
            {
                _CurrNursingVal = value;
                RaisePropertyChanged("CurrNursingVal");
            }
        }

        //List<wardarea> _AllArea;
        ///// <summary>
        ///// 区域字典列表  2023-05-24
        ///// </summary>
        //public List<wardarea> AllArea
        //{
        //    get
        //    {
        //        return _AllArea;
        //    }
        //    set
        //    {
        //        _AllArea = value;
        //        RaisePropertyChanged("AllArea");
        //    }
        //}

        //private DataTemplate _BatchVitalSignDataTemplate;
        ///// <summary>
        ///// 护理生命体征批量录入主题模板  2023-05-24
        ///// </summary>
        //public DataTemplate BatchVitalSignDataTemplate
        //{
        //    get => _BatchVitalSignDataTemplate;
        //    set
        //    {
        //        _BatchVitalSignDataTemplate = value;
        //        RaisePropertyChanged(nameof(BatchVitalSignDataTemplate));
        //    }
        //}

        //private wardarea _CurArea;
        ///// <summary>
        ///// 当前选择区域  2023-05-24
        ///// </summary>
        //public wardarea CurArea
        //{
        //    get
        //    {
        //        return _CurArea;
        //    }
        //    set
        //    {
        //        _CurArea = value;
        //        RaisePropertyChanged("CurArea");
        //    }
        //}
        /// <summary>
        /// 列表控件  2023-05-24
        /// </summary>
        public System.Windows.Controls.DataGrid dataGrid { get; set; }

        /// <summary>
        /// 参数范围列表  2023-05-24
        /// </summary>
       // public List<nursing_column> DictParamlist { get; set; }

        /// <summary>
        /// 搜索命令  2023-05-24
        /// </summary>
        public DelegateCommand<object> SerchCommand { get; set; }

        /// <summary>
        /// 护理内容编辑命令  2023-05-24
        /// </summary>
        public DelegateCommand<object> BQGCCommand { get; set; }
        /// <summary>
        /// 改变搜索条件命令  2023-05-24
        /// </summary>
        public DelegateCommand<object> ChangeCommand { get; set; }

        /// <summary>
        /// 保存文本  2023-05-24
        /// </summary>
        public DelegateCommand<object> SaveNursingCommand { get; set; }

        /// <summary>
        /// 加载更多  2023-05-24
        /// </summary>
        public DelegateCommand<object> MostCommand { get; set; }

        /// <summary>
        /// 血压单元编辑命令  2023-05-24
        /// </summary>
        public DelegateCommand<object> CalculCommand { get; set; }
        /// <summary>
        /// 导出命令 2022-05-26 
        /// </summary>
        public DelegateCommand<object> OutPutCommad { get; private set; }
        #endregion

        #region 方法

        /// <summary>
        /// 构造方法  2023-05-24
        /// </summary>
        /// <param name="container"></param>
        /// <param name="eventAggregator"></param>
        public BatchVitalSignViewModel(IContainerExtension container, IEventAggregator eventAggregator) : base(container, eventAggregator)
        {
            this.SerchCommand = new DelegateCommand<object>(OnSerch);
            this.BQGCCommand = new DelegateCommand<object>(OnBQGCCommand);
            this.ChangeCommand = new DelegateCommand<object>(OnChangeCommand);
            this.MostCommand = new DelegateCommand<object>(OnBQGCCommand);
            this.SaveNursingCommand = new DelegateCommand<object>(OnSaveNursingCommand);
            this.CalculCommand = new DelegateCommand<object>(OnCalculCommand);
            this.OutPutCommad = new DelegateCommand<object>(OnOutPut);
           // DictService = container.Resolve<IDictServices>();
           // NursingRecordService = this.container.Resolve<INursingRecordServices>();
           // DictParamlist = DictService.GetDicByTableName<nursing_column>()?.FindAll(c => "NursingIntegrationColumListQJTheme".Equals(c.ID));
           // AllArea = DictService.GetDicByTableName<wardarea>().FindAll(c => c.HOSPITALCOMPOUNDCODE.Contains(CurrUser.SelectHospitalCode));
            #region 默认值
            _ChooseDATE = DateTime.Now;
            RaisePropertyChanged("ChooseDATE");
           // _CurArea = AllArea?.Find(c => c.AREANAME.Contains("抢救"));
            RaisePropertyChanged("CurArea");
            #endregion
        }

        /// <summary>
        /// 整体初始化  2023-05-24
        /// </summary>
        public void Init()
        {
            //dataGrid.BeginningEdit += BeginningEdit;
            //dataGrid.CurrentCellChanged += AssociatedObject_CurrentCellChanged;
            //dataGrid.CellEditEnding += EndingCellEdit;
            //dataGrid.PreviewKeyDown += AssociatedObject_PreviewKeyDown;
            //dataGrid.PreparingCellForEdit += DataGrid_PreparingCellForEdit;
            OnSerch(null);
        }

        /// <summary>
        /// 刷新列表  2023-05-24
        /// </summary>
        /// <param name="arg"></param>
        void OnSerch(object arg)
        {

            //if (CurArea == null || ChooseDATE == DateTime.MinValue)
            //{
            //    NotifiactionHelper.Instance.ShowMessage("失败", "请选择日期与区域", EnumPromptType.Error);
            //    return;
            //}

            LoadData();
            this.CanEditData = true;
        }

        /// <summary>
        /// 整合数据，实体处理加载  2023-05-24
        /// </summary>
        private void LoadData()
        {
            List<NursingPatInfo> ALLPatientList = new List<NursingPatInfo>();
            List<Nursing_Column_Record> ALLNurRecordList = new List<Nursing_Column_Record>();
            dynamic param = new System.Dynamic.ExpandoObject();
            param.DateTime = ChooseDATE.ToString("yyyy-MM-dd HH:mm:ss");
            param.Area = "1";
            var common = InterfaceToolHelper.Instance.CallJsConvertDynamic("NursingBatchVitalSign", param);
            if (common.Result)
            {
                var data = JsonConvert.DeserializeObject<BathVitalSignModel>(common.ReturnData.ToString());
                if (data != null)
                {
                    ALLPatientList = data.PatInfo;
                    ALLNurRecordList = data.NursingRecord;
                }
            }
            try
            {
                if (dataGrid != null)
                {
                    #region datagrid绑定数据，绑定事件
                    var ListProper = typeof(VitalSignRecord).GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
                    List<VitalSignRecord> tempList = new List<VitalSignRecord>();
                    for (int ip = 0; ip < ALLPatientList.Count; ip++)//患者
                    {
                        VitalSignRecord tempRow = new VitalSignRecord();
                        NursingPatInfo Pat = ALLPatientList[ip];
                        Nursing_Column_Record Nursing = null;
                        if (ALLNurRecordList != null)
                        {
                            Nursing = ALLNurRecordList.Find(c => c.PVID == Pat.PVID);
                        }
                        for (int i = 0; i < ListProper.Count; i++)
                        {
                            string parmid = ListProper[i].Name;
                            object o = PropertyHelper.FindProValue<object>(parmid, Pat, Nursing);
                            PropertyHelper.FindProSetValue(parmid, o, tempRow);
                        }
                        tempList.Add(tempRow);
                    }
                    AllVitalSignRecord = tempList;
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error("整体录入加载报错：" + ex.ToString());
            }
        }

        /// <summary>
        /// 编辑数据  2023-05-24
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="isShowNotifiaction"></param>
        /// <param name="isClearCA"></param>
        /// <returns></returns>
        private bool UpdateRowData(VitalSignRecord putObj)
        {
            bool result = false;
            try
            {
                Type tp = typeof(Nursing_Column_Record);
                var pro = tp.GetProperty(putObj.ColumnName);
                Nursing_Column_Record temp = new Nursing_Column_Record();
                pro.SetValue(temp, putObj.EndText, null);
                ResponseApi<CommonResult> me = null;
                if (putObj.ID == null)
                {//新增
                    temp.ID = Guid.NewGuid().ToString();
                    temp.PVID = putObj.PVID;
                    temp.ISMAIN = 1;
                    temp.RECORD_TIME = ChooseDATE;
                    //temp.AREACODE = CurArea.AREACODE;
                    //temp.CREATE_USER_NAME = CurrUser.DISPLAYNAME;
                    //temp.CREATE_USER_ID = CurrUser.ID;
                    temp.RECORDNUM = 1;
                    temp.CREATE_TIME = DateTime.Now;
                    //me = NursingRecordService.AddNursingColumnRecord(temp);
                    PutIdToDataGrid(temp.ID);
                }
                else
                {
                    temp.ID = putObj.ID;
                    temp.ColumnName = putObj.ColumnName;
                    //me = NursingRecordService.UpdateNursingColumnRecord(temp);
                }
                #region 保存数据库
                result = me.code == -1 || !me.data.Result;
                if (me.code == -1 || !me.data.Result)
                {
                    NotifiactionHelper.Instance.ShowMessage("失败", "保存失败,请刷新界面", EnumPromptType.Error);

                }
                else
                {
                    NotifiactionHelper.Instance.ShowMessage("成功", "保存成功！", EnumPromptType.Success);
                    result = true;
                }
                #endregion
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error(ex);
            }
            return result;
        }

        /// <summary>
        /// 新增体征后，将数据刷新替换到界面  2023-05-24
        /// </summary>
        public void PutIdToDataGrid(string ID)
        {
            if (CurrVitSign != null)
            {
                CurrVitSign.ID = ID;
                //CurrVitSign.CREATE_USER_ID = CurrUser.ID;
                //CurrVitSign.RECORD_TIME = ChooseDATE;
                //CurrVitSign.AREACODE = CurArea.AREACODE;
                //CurrVitSign.CREATE_USER_NAME = CurrUser.DISPLAYNAME;
                //CurrVitSign.CREATE_USER_ID = CurrUser.ID;
            }
        }
        #endregion

        #region 命令处理方法

        /// <summary>
        /// 改变搜索参数  2023-05-24
        /// </summary>
        /// <param name="o"></param>
        private void OnChangeCommand(object o)
        {
            this.CanEditData = false;
        }

        /// <summary>
        /// 编辑护理记录内容  2023-05-24
        /// </summary>
        private void OnBQGCCommand(object o)
        {
            try
            {
                if (CurrVitSign != null)
                {
                    if (string.IsNullOrEmpty(CurrVitSign.ID))
                    {
                        VitalSignRecord putObj = new VitalSignRecord();
                        putObj.BeforeText = "";
                        putObj.PVID = CurrVitSign.PVID;
                        putObj.ID = CurrVitSign.ID;
                        putObj.ColumnName = nameof(CurrVitSign.NURSING);
                        putObj.EndText = "";
                        if (UpdateRowData(putObj))
                        {
                            ShowNursingDiag();
                        }
                    }
                    else
                    {
                        ShowNursingDiag();
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error("编辑护理内容出现问题：" + ex.Message);
                NotifiactionHelper.Instance.ShowMessage("提示", "编辑护理记录出现问题，请刷新界面！", EnumPromptType.Error);
            }
        }

        /// <summary>
        /// 显示编辑护理内容弹框  2023-05-24
        /// </summary>
        private void ShowNursingDiag()
        {
            //Nursing_Column_Record mainRecord = new Nursing_Column_Record() { PVID = CurrVitSign.PVID, ID = CurrVitSign.ID, CREATE_USER_NAME = CurrVitSign.CREATE_USER_NAME, CREATE_USER_ID = CurrVitSign.CREATE_USER_ID, AREACODE = CurrVitSign.AREACODE, RECORD_TIME = CurrVitSign.RECORD_TIME, NURSING = CurrVitSign.NURSING };
            //NursingRecordColumnViewModel model = new NursingRecordColumnViewModel(this.container, this.eventAggregator);
            //model.Nursing_Column_Record = mainRecord;
            //NursingRecordColumnView from = new NursingRecordColumnView(model);
            //bool? diares = from.ShowDialog();
            //if (diares == true)
            //{
            //    CurrVitSign.NURSING = model.Nursing_Column_Record.NURSING;
            //    CurrNursingVal = CurrVitSign.NURSING;
            //}
        }

        /// <summary>
        /// 保存护理记录内容  2023-05-24
        /// </summary>
        private void OnSaveNursingCommand(object o)
        {
            try
            {
                if (CurrVitSign != null)
                {
                    VitalSignRecord putObj = new VitalSignRecord();
                    putObj.BeforeText = CurrVitSign.NURSING;
                    putObj.PVID = CurrVitSign.PVID;
                    putObj.ID = CurrVitSign.ID;
                    putObj.ColumnName = nameof(CurrVitSign.NURSING);
                    putObj.EndText = CurrNursingVal;
                    if (UpdateRowData(putObj))
                    {
                        CurrVitSign.NURSING = CurrNursingVal;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error("编辑护理内容出现问题：" + ex.Message);
            }
        }
        /// <summary>
        /// 一个单元格多个列特殊处理【血压】  2023-05-24
        /// </summary>
        /// <param name="o"></param>
        public void OnCalculCommand(object o)
        {
            //if (o is TextBox t)
            //{
            //    if (!string.IsNullOrEmpty(t.Name))
            //    {
            //        VitalSignRecord putObj = new VitalSignRecord();
            //        putObj.PVID = CurrVitSign.PVID;
            //        putObj.ID = CurrVitSign.ID;
            //        putObj.ColumnName = t.Name;
            //        putObj.EndText = t.Text;
            //        switch (putObj.ColumnName)
            //        {
            //            case "SSY":
            //                putObj.BeforeText = CurrVitSign.SSY ?? "";
            //                putObj.SSY = putObj.EndText;
            //                break;
            //            case "SZY":
            //                putObj.BeforeText = CurrVitSign.SZY ?? "";
            //                putObj.SZY = putObj.EndText;
            //                break;
            //            default:
            //                return;
            //        }
            //        if (!putObj.BeforeText.Equals(putObj.EndText))
            //        {
            //            if (UpdateRowData(putObj))
            //            {
            //                if (t.Name == "SSY")
            //                {
            //                    CurrVitSign.SSY = t.Text;
            //                }
            //                else if (t.Name == "SZY")
            //                {
            //                    CurrVitSign.SZY = t.Text;
            //                }
            //            }
            //        }
            //    }
            //}
        }

        /// <summary>
        /// 单元格编辑前逻辑处理-记录  2023-05-24
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
        //{
        //    if (CurrVitSign == null)
        //    {
        //        return;
        //    }
        //    EditObj = CurrVitSign;
        //    if (e.Column.GetType() == typeof(DataGridTextColumn))
        //    {
        //        EditObj.ColumnName = ((DataGridTextColumn)e.Column).SortMemberPath;
        //        EditObj.BeforeText = (e.Column.GetCellContent(e.Row) as TextBlock).Text;
        //    }
        //    else if (e.Column.GetType() == typeof(DataGridTemplateColumn))
        //    {
        //        #region 模板列找修改的列名
        //        try
        //        {
        //            DataGridTemplateColumn templeColumn = (DataGridTemplateColumn)e.Column;
        //            FrameworkElement element = templeColumn.GetCellContent(e.Row);
        //            object element2 = templeColumn.CellTemplate.FindName("DropTempText", element);
        //            if (element2 != null && element2.GetType() == typeof(TextBlock))
        //            {
        //                TextBlock tb = (TextBlock)element2;
        //                System.Windows.Data.BindingExpression kl = tb.GetBindingExpression(TextBlock.TextProperty);
        //                Binding hhh = kl.ParentBinding;
        //                EditObj.ColumnName = hhh.Path.Path;
        //                EditObj.BeforeText = tb.Text;
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            LogHelper.GetLogHelper().Error("BeginEdit模板列报错：" + ex);
        //        }
        //        #endregion
        //    }
        //}

        /// <summary>
        /// 单元格编辑后逻辑处理-保存  2023-05-24
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void EndingCellEdit(object sender, DataGridCellEditEndingEventArgs e)
        //{
        //    try
        //    {
        //        if (e.Column == null || EditObj == null || sender == null)
        //            return;
        //        if (EditObj.ColumnName.Equals("XUEYA"))
        //            return;
        //        TextBox box = null;
        //        //ComboBoxEdit editbox = null;
        //        //if (e.Column is DataGridTextColumn)
        //        //{
        //        //    box = (e.EditingElement as TextBox);
        //        //    if (box != null)
        //        //        EditObj.EndText = box.Text.ToString();
        //        //}
        //        //else if (e.Column is DataGridTemplateColumn)
        //        //{
        //        //    editbox = UIHelpers.GetVisualChild<ComboBoxEdit>(e.EditingElement);
        //        //    if (editbox != null)
        //        //        EditObj.EndText = editbox.Text.ToString();
        //        //}
        //        if (!string.IsNullOrEmpty(EditObj?.ColumnName))
        //        {
        //            if (!EditObj.BeforeText.Equals(EditObj.EndText))
        //            {
        //                if (!ColumRule(EditObj.ColumnName, EditObj.EndText, out string MESSAGE_INFO))
        //                {
        //                    //notifiaction.AddNotifiaction(new NotifiactionModel() { Title = "提示", Content = MESSAGE_INFO, NotifiactionType = EnumPromptType.Warn });
        //                    //if (box != null)
        //                    //{
        //                    //    box.Text = EditObj.BeforeText;
        //                    //}
        //                    //else if (editbox != null)
        //                    //{
        //                    //    editbox.Text = EditObj.BeforeText;
        //                    //}
        //                    return;
        //                }
        //                UpdateRowData(EditObj);
        //            }
        //        }
        //        EditObj = null;
        //    }
        //    catch (Exception ex)
        //    {
        //        LogHelper.GetLogHelper().Error("生命体征整体录入报错" + ex);
        //    }
        //}

        /// <summary>
        /// DataGrid 绘制事件  2023-05-24
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void DataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
        //{
        //    //if (!string.IsNullOrEmpty(CurItem))
        //    //{
        //    //    (e.Column.GetCellContent(e.Row) as TextBox).Text = CurItem;
        //    //    CurItem = string.Empty;
        //    //}
        //}

        /// <summary>
        /// 选中的单元格改变  2023-05-24
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void AssociatedObject_CurrentCellChanged(object sender, System.EventArgs e)
        //{
        //    try
        //    {
        //        var item = dataGrid.CurrentCell;
        //        if (dataGrid.CurrentItem != null && dataGrid.CurrentItem is VitalSignRecord)
        //        {
        //            CurrVitSign = (VitalSignRecord)dataGrid.CurrentItem;
        //        }
        //        if (item != null)
        //        {
        //            dataGrid.BeginEdit();
        //        }
        //    }
        //    catch (System.Exception ex)
        //    {
        //        LogHelper.GetLogHelper().Error(ex);
        //        return;
        //    }
        //}

        /// <summary>
        /// 根据行、列索引取的对应单元格对象  2023-05-24
        /// </summary>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        //public DataGridCell GetCell(DataGrid grid, int row, int column)
        //{
        //    if (column >= dataGrid.Columns.Count)
        //    {
        //        return null;
        //    }
        //    DataGridRow rowContainer = GetRow(grid, row);
        //    if (rowContainer != null)
        //    {
        //        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer)?.FirstOrDefault();
        //        if (presenter == null)
        //        {
        //            grid.ScrollIntoView(rowContainer, grid.Columns[column]);
        //            presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer)?.FirstOrDefault();
        //        }
        //        DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
        //        return cell;
        //    }
        //    return null;
        //}

        /// <summary>
        /// 根据行索引取的行对象  2023-05-24
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        //public DataGridRow GetRow(DataGrid grid, int index)
        //{
        //    DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        //    if (row == null)
        //    {
        //        grid.ScrollIntoView(grid.Items[index]);
        //        row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        //    }
        //    return row;
        //}

        /// <summary>
        /// 查找指定类型兄弟控件  2023-05-24
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List<T> GetVisualBrother<T>(DependencyObject obj) where T : DependencyObject
        {
            DependencyObject parent = VisualTreeHelper.GetParent(obj);
            return GetVisualChild<T>(parent);
        }

        /// <summary>
        /// 查找子控件  2023-05-24
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="visual"></param>
        /// <returns></returns>
        private static List<T> GetVisualChild<T>(DependencyObject visual) where T : DependencyObject
        {
            if (visual == null)
            {
                return null;
            }
            List<T> BroList = new List<T>();
            var count = VisualTreeHelper.GetChildrenCount(visual);
            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(visual, i);

                var childOfTypeT = child as T ?? GetVisualChild<T>(child)?.FirstOrDefault();
                if (childOfTypeT != null)
                {
                    BroList.Add(childOfTypeT);
                }
            }
            return BroList;
        }

        /// <summary>
        /// 按键触发事件  2023-05-24
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AssociatedObject_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            //try
            //{
            //    var uie = e.OriginalSource as System.Windows.UIElement;
            //    switch (e.Key)
            //    {
            //        case Key.Enter:
            //            if (uie is TextBox tb)
            //            {
            //                if (tb.Name == "SSY")
            //                {
            //                    TextBox TbSZY = GetVisualBrother<TextBox>(tb)?.Find(c => c.Name == "SZY");
            //                    TbSZY?.Focus();
            //                }
            //                else
            //                {
            //                    NextCellEdit();
            //                }
            //            }
            //            else
            //            {
            //                NextCellEdit();
            //            }
            //            break;
            //        case Key.Up:
            //            CellEditByXY(0, -1);
            //            break;
            //        case Key.Left:
            //            CellEditByXY(-1, 0);
            //            break;
            //        case Key.Right:
            //            if (uie is TextBox tb2)
            //            {
            //                if (tb2.Name == "SSY")
            //                {
            //                    TextBox TbSZY = GetVisualBrother<TextBox>(tb2)?.Find(c => c.Name == "SZY");
            //                    TbSZY?.Focus();
            //                }
            //                else
            //                {
            //                    CellEditByXY(1, 0);
            //                }
            //            }
            //            else
            //            {
            //                CellEditByXY(1, 0);
            //            }
            //            break;
            //        default:
            //            return;
            //    }
            //    e.Handled = true;
            //}
            //catch (Exception ex)
            //{
            //    LogHelper.GetLogHelper().Error("按键处理事件报错" + ex);
            //}
        }

        /// <summary>
        /// 跳转编辑下一个单元格  2023-05-24
        /// </summary>
        private void NextCellEdit()
        {
            CellEditByXY(0, 1);
        }

        /// <summary>
        /// 通过上下左右移动选中单元，换行或换列处理  2023-05-24
        /// </summary>
        /// <param name="x">右偏移</param>
        /// <param name="y">下偏移</param>
        private void CellEditByXY(int x = 0, int y = 0)
        {
            //int canEditCol = 4;
            //if (x != 0 && y != 0)
            //{
            //    return;
            //}
            //Int32 row = this.dataGrid.Items.IndexOf(this.dataGrid.CurrentItem);
            //Int32 Col = this.dataGrid.Columns.IndexOf(this.dataGrid.CurrentColumn);
            //if (x > 0)
            //{//右
            //    if (Col >= this.dataGrid.Columns.Count - 1)
            //    {
            //        row += 1;
            //        Col = canEditCol;
            //    }
            //    else
            //    {
            //        Col += 1;
            //    }
            //}
            //if (x < 0)
            //{//左
            //    if (Col == canEditCol)
            //    {
            //        row -= 1;
            //        Col = this.dataGrid.Columns.Count - 1;
            //    }
            //    else
            //    {
            //        Col -= 1;
            //    }
            //}
            //if (y > 0)
            //{//下
            //    if (row >= this.dataGrid.Items.Count - 1)
            //    {
            //        row = 0;
            //        Col = Col + 1;
            //    }
            //    else
            //    {
            //        row += 1;
            //    }
            //}
            //if (y < 0)
            //{//上
            //    if (row == 0)
            //    {
            //        row = this.dataGrid.Items.Count - 1;
            //        Col = Col - 1;
            //        if (Col < canEditCol)
            //        {
            //            return;
            //        }
            //    }
            //    else
            //    {
            //        row -= 1;
            //    }
            //}
            //var Nextcell = GetCell(this.dataGrid, row, Col);
            //if (Nextcell != null)
            //{
            //    Nextcell.Focus();
            //    this.dataGrid.BeginEdit();
            //}
        }
        /// <summary>
        /// 指定列输入校验  2023-05-24
        /// </summary>
        /// <param name="ColumnName"></param>
        /// <param name="EndText"></param>
        /// <returns></returns>
        private bool ColumRule(string ColumnName, string EndText, out string res)
        {
            //var GetParmModel = DictParamlist?.Find(c => c.COLUMN_NAME == ColumnName);
            //if (GetParmModel != null && !string.IsNullOrEmpty(GetParmModel.COLUMN_RULE))
            //{
            //    System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(GetParmModel.COLUMN_RULE);
            //    if (!regx.IsMatch(EndText))
            //    {
            //        res = GetParmModel.MESSAGE_INFO;
            //        return false;
            //    }
            //}
            res = string.Empty;
            return true;
        }

        /// <summary>
        /// 导出患者列表数据  2023-05-25 
        /// </summary>
        /// <param name="obj"></param>
        public void OnOutPut(object obj)
        {
            //if (dataGrid != null)
            //{
            //    DataTable dt = new DataTable();
            //    dt = DataGridBuildTable(dataGrid);
            //    //System.Windows.Forms.SaveFileDialog dialog1 = new System.Windows.Forms.SaveFileDialog();
            //    //dialog1.FileName = string.Concat(new string[]
            //    //{
            //    //        Environment.GetFolderPath(Environment.SpecialFolder.Desktop),  "\\整体录入(", this.ChooseDATE.ToString("yyyy年MM月dd日hh时mm分ss秒"), ").xls"
            //    //});
            //    //dialog1.Filter = @"xls files(*.xls)|*.xls";
            //    //if (dialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //    //{
            //    //    ExcelHelper.Export(dt, string.Concat(new string[] { "整体录入(", this.ChooseDATE.ToString("yyyy-MM-dd hh:mm:ss"), ")" }), dialog1.FileName);
            //    //}
            //    //notifiaction.AddNotifiaction(new NotifiactionModel() { Title = "提示", Content = "整体录入查询数据已导出！", NotifiactionType = EnumPromptType.Warn });
            //}
        }
        /// <summary>
        /// DataGrid列表,生成DataTable  2023-05-25 
        /// </summary>
        /// <param name="dg"></param>
        /// <returns></returns>
        //public DataTable DataGridBuildTable(DataGrid dg)
        //{
        //    List<string> ListBind = new List<string>();
        //    DataTable dt = new DataTable();
        //    for (int i = 0; i < dg.Columns.Count; i++)
        //    {
        //        DataGridColumn dgc = dg.Columns[i];
        //        ListBind.Add(dgc.HeaderStringFormat.ToString());
        //        DataColumn dc = new DataColumn(dgc.Header.ToString());
        //        dt.Columns.Add(dc);
        //    }
        //    return PatList2Table(dt, ListBind);
        //}
        /// <summary>
        /// 对照生成列表
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public DataTable PatList2Table(DataTable dt, List<string> ListBind)
        {
            for (int i = 0; i < AllVitalSignRecord.Count; i++)
            {
                DataRow dr = dt.NewRow();
                VitalSignRecord model = AllVitalSignRecord[i];
                for (int j = 0; j < ListBind.Count; j++)
                {
                    string pname = ListBind[j];
                    PropertyInfo property = typeof(VitalSignRecord).GetProperty(pname);
                    string value = "";
                    if (property != null)
                    {
                        object proobj = property.GetValue(model, null);
                        if (proobj != null)
                        {
                            value = proobj.ToString();
                        }
                        dr[j] = value;
                    }
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
        #endregion

        #region 接口实现方法-导航相关

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
        }
        #endregion
    }

}